﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using HuaCheng.DataAccess;
using HuaCheng.CommonClass;

namespace HuaCheng.Views
{
    /// <summary>
    /// 用户管理
    /// </summary>
    public partial class UserManagement : Form
    {
        public UserManagement()
        {
            InitializeComponent();
            //绑定权限勾选菜单
            BindMenu();
            //用户组选项卡中绑定用户组名
            Bindyhzname(cbxGroupyhz);
            //用户选项卡中绑定用户组名
            Bindyhzname(cbxYhz);       
        }
        /// <summary>
        /// 用户组树形列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void usergroupTv_AfterSelect(object sender, TreeViewEventArgs e)
        {
            switch(e.Node.Name)
            {
                case "addugroup":
                    gbGroup.Text = ResourceCulture.GetString("AddUserGroup");
                    cbAllcheck.Enabled=true;
                    clbAuthority.Enabled = true;
                    tbGroupremark.Enabled = true;
                    break;
                case "updateugroup":
                    gbGroup.Text = ResourceCulture.GetString("EditUserGroup");
                    cbAllcheck.Enabled=true;
                    clbAuthority.Enabled = true;
                    tbGroupremark.Enabled = true;
                    break;
                case "deleteugroup":
                    gbGroup.Text = ResourceCulture.GetString("DeleteUserGroup");
                    cbAllcheck.Enabled=false;
                    clbAuthority.Enabled=false;
                    tbGroupremark.Enabled = false;
                    break;
            }
            ClearCntrValue(this.gbGroup);
            allcheckCb_Clear();
            authorityClb_ClearChecked();
        }
        /// <summary>
        /// 用户树形列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void userTv_AfterSelect(object sender, TreeViewEventArgs e)
        {
            ClearCntrValue(this.gbUser);
            switch (e.Node.Name)
            {
                case "adduser":
                    gbUser.Text = ResourceCulture.GetString("AddUser");
                    tbPwd.Enabled = true;
                    tbConfirmpwd.Enabled = true;
                    tbTel.Enabled = true;
                    tbEmail.Enabled = true;
                    tbRemark.Enabled = true;
                    break;
                case "updateuser":
                    gbUser.Text = ResourceCulture.GetString("EditUser");
                    tbPwd.Enabled = false;
                    tbConfirmpwd.Enabled = false;
                    tbTel.Enabled = true;
                    tbEmail.Enabled = true;
                    tbRemark.Enabled = true;
                    break;
                case "deleteuser":
                    gbUser.Text = ResourceCulture.GetString("DeleteUser");
                    tbPwd.Enabled = false;
                    tbConfirmpwd.Enabled = false;
                    tbTel.Enabled = false;
                    tbEmail.Enabled = false;
                    tbRemark.Enabled = false;
                    break;
            }
        }       
        /// <summary>
        /// 用户组提交按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void groupsubmit_Click(object sender, EventArgs e)
        {
            switch (gbGroup.Text)
            {
                case "添加用户组":
                    if (cbxGroupyhz.Text.Trim() == "")
                    {
                        MessageBox.Show("用户组不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (!HasSelected())
                    {
                        MessageBox.Show("权限选择不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (UserGroupHelper.UserGroupIsExist(cbxGroupyhz.Text))
                    {
                        MessageBox.Show("存在相同的用户组名！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        UserGroup usergroup = new UserGroup()
                        {
                            GroupName = cbxGroupyhz.Text,
                            PopedomCodeex = GetChecked(clbAuthority),
                            Remark = tbGroupremark.Text,
                        };
                        UserGroupHelper.InsertUserGroup(usergroup);
                        MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        ClearCntrValue(this.gbGroup);
                        allcheckCb_Clear();
                        authorityClb_ClearChecked();
                        Bindyhzname(cbxGroupyhz);
                        Bindyhzname(cbxYhz);
                    }
                    break;
                case "Add User Group":
                    if (cbxGroupyhz.Text.Trim() == "")
                    {
                        MessageBox.Show("User Group can't be empty！", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (!HasSelected())
                    {
                        MessageBox.Show("Permission can't be empty！", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (UserGroupHelper.UserGroupIsExist(cbxGroupyhz.Text))
                    {
                        MessageBox.Show("Name has existed！", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        UserGroup usergroup = new UserGroup()
                        {
                            GroupName = cbxGroupyhz.Text,
                            PopedomCodeex = GetChecked(clbAuthority),
                            Remark = tbGroupremark.Text,
                        };
                        UserGroupHelper.InsertUserGroup(usergroup);
                        MessageBox.Show("Success", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        ClearCntrValue(this.gbGroup);
                        allcheckCb_Clear();
                        authorityClb_ClearChecked();
                        Bindyhzname(cbxGroupyhz);
                        Bindyhzname(cbxYhz);
                    }
                    break;
                case "修改用户组":
                    if (cbxGroupyhz.Text.Trim() == "")
                    {
                        MessageBox.Show("用户组不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (!HasSelected())
                    {
                        MessageBox.Show("权限选择不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (UserGroupHelper.UserGroupIsExist(cbxGroupyhz.Text)==false)
                    {
                        MessageBox.Show("用户组名不存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        UserGroup ugroup = new UserGroup()
                        {
                            GroupName = cbxGroupyhz.Text,
                            PopedomCodeex = GetChecked(clbAuthority),
                            Remark = tbGroupremark.Text
                        };
                        UserGroupHelper.UpdateUserGroup(ugroup);
                        MessageBox.Show("修改成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        ClearCntrValue(this.gbGroup);
                        allcheckCb_Clear();
                        authorityClb_ClearChecked();
                        Bindyhzname(cbxGroupyhz);
                        Bindyhzname(cbxYhz);
                    }
                    break;
                case "Edit User Group":
                    if (cbxGroupyhz.Text.Trim() == "")
                    {
                        MessageBox.Show("User Group can't be empty！", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (!HasSelected())
                    {
                        MessageBox.Show("Permission can't be empty！", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (UserGroupHelper.UserGroupIsExist(cbxGroupyhz.Text)==false)
                    {
                        MessageBox.Show("Don't exist this user group！", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        UserGroup ugroup = new UserGroup()
                        {
                            GroupName = cbxGroupyhz.Text,
                            PopedomCodeex = GetChecked(clbAuthority),
                            Remark = tbGroupremark.Text
                        };
                        UserGroupHelper.UpdateUserGroup(ugroup);
                        MessageBox.Show("Success", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        ClearCntrValue(this.gbGroup);
                        allcheckCb_Clear();
                        authorityClb_ClearChecked();
                        Bindyhzname(cbxGroupyhz);
                        Bindyhzname(cbxYhz);
                    }
                    break;
                case "删除用户组":
                    if (cbxGroupyhz.Text.Trim() == "")
                    {
                        MessageBox.Show("用户组不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (UserGroupHelper.UserGroupIsExist(cbxGroupyhz.Text) == false)
                    {
                        MessageBox.Show("用户组名不存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (UserInfoHelper.HasUser(cbxGroupyhz.Text))
                    {
                            MessageBox.Show("用户组中存在用户，无法删除该用户组！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (MessageBox.Show("确定删除?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        UserGroupHelper.DeleteUserGroup(cbxGroupyhz.Text);
                        MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        ClearCntrValue(this.gbGroup);
                        allcheckCb_Clear();
                        authorityClb_ClearChecked();
                        Bindyhzname(cbxGroupyhz);
                        Bindyhzname(cbxYhz);
                    }
                    break;
                case "Delete User Group":
                    if (cbxGroupyhz.Text.Trim() == "")
                    {
                        MessageBox.Show("User Group can't be empty！", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (UserGroupHelper.UserGroupIsExist(cbxGroupyhz.Text) == false)
                    {
                        MessageBox.Show("Don't exist this user group！", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (UserInfoHelper.HasUser(cbxGroupyhz.Text))
                    {
                            MessageBox.Show("User Group exists users, detele failed！", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (MessageBox.Show("Delete?", "Tip", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        UserGroupHelper.DeleteUserGroup(cbxGroupyhz.Text);
                        MessageBox.Show("Success", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        ClearCntrValue(this.gbGroup);
                        allcheckCb_Clear();
                        authorityClb_ClearChecked();
                        Bindyhzname(cbxGroupyhz);
                        Bindyhzname(cbxYhz);
                    }
                    break;
            }
        }
        /// <summary>
        /// 用户提交按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void usersubmit_Click(object sender, EventArgs e)
        {
            switch (gbUser.Text)
            {
                case "添加用户":
                    if (cbxYhm.Text.Trim() == "")
                    {
                        MessageBox.Show("用户不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (UserGroupHelper.UserGroupIsExist(cbxYhz.Text) == false)
                    {
                        MessageBox.Show("用户组不存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (UserInfoHelper.IsExist(cbxYhm.Text))
                    {
                        MessageBox.Show("存在相同的用户名！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (tbPwd.Text.Trim() == "")
                    {
                        MessageBox.Show("密码不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (tbConfirmpwd.Text.Trim() == "")
                    {
                        MessageBox.Show("确认密码不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (tbPwd.Text.Trim() != tbConfirmpwd.Text.Trim())
                    {
                        MessageBox.Show("两次输入的新密码不一致，请重新输入！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (tbTel.Text.Trim() == "")
                    {
                        MessageBox.Show("电话号码不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (!Util.IsTel(tbTel.Text))
                    {
                        MessageBox.Show("电话格式错误！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (tbEmail.Text.Trim() == "")
                    {
                        MessageBox.Show("Email不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (!Util.IsEmail(tbEmail.Text))
                    {
                        MessageBox.Show("Email格式错误！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        UserInfo userinfo = new UserInfo()
                            {
                                UserName = cbxYhm.Text,
                                PassWord = tbPwd.Text,
                                Telephone=tbTel.Text,
                                Email = tbEmail.Text,
                                Groupname = cbxYhz.Text,
                                Remark = tbRemark.Text
                            };
                        UserInfoHelper.InsertUser(userinfo);
                        MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        ClearCntrValue(this.gbUser);
                        Bindyhzname(cbxYhz);
                    }
                    break;
                case "Add User":
                    if (cbxYhm.Text.Trim() == "")
                    {
                        MessageBox.Show("Username empty！", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (UserGroupHelper.UserGroupIsExist(cbxYhz.Text) == false)
                    {
                        MessageBox.Show("User group doesn't exist！", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (UserInfoHelper.IsExist(cbxYhm.Text))
                    {
                        MessageBox.Show("Username exists！", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (tbPwd.Text.Trim() == "")
                    {
                        MessageBox.Show("Password empty！", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (tbConfirmpwd.Text.Trim() == "")
                    {
                        MessageBox.Show("ConfirmPwd empty！", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (tbPwd.Text.Trim() != tbConfirmpwd.Text.Trim())
                    {
                        MessageBox.Show("The password you enter twice must be the same！", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (tbTel.Text.Trim() == "")
                    {
                        MessageBox.Show("TEL empty！", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (!Util.IsTel(tbTel.Text))
                    {
                        MessageBox.Show("Please input the correct TEL！", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (tbEmail.Text.Trim() == "")
                    {
                        MessageBox.Show("Email empty！", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (!Util.IsEmail(tbEmail.Text))
                    {
                        MessageBox.Show("Please input the correct email！", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        UserInfo userinfo = new UserInfo()
                        {
                            UserName = cbxYhm.Text,
                            PassWord = tbPwd.Text,
                            Telephone = tbTel.Text,
                            Email = tbEmail.Text,
                            Groupname = cbxYhz.Text,
                            Remark = tbRemark.Text
                        };
                        UserInfoHelper.InsertUser(userinfo);
                        MessageBox.Show("Success", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        ClearCntrValue(this.gbUser);
                        Bindyhzname(cbxYhz);
                    }
                    break;
                case "修改用户":
                    if (cbxYhm.Text.Trim() == "")
                    {
                        MessageBox.Show("用户不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (UserGroupHelper.UserGroupIsExist(cbxYhz.Text) == false)
                    {
                        MessageBox.Show("用户组不存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (tbTel.Text.Trim() == "")
                    {
                        MessageBox.Show("电话号码不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (!Util.IsTel(tbTel.Text))
                    {
                        MessageBox.Show("电话格式错误！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (tbEmail.Text.Trim() == "")
                    {
                        MessageBox.Show("Email不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (!Util.IsEmail(tbEmail.Text))
                    {
                            MessageBox.Show("Email格式错误！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);//不匹配则弹出”邮箱错误“
                    }
                    else 
                    {
                            UserInfo userinfo = new UserInfo()
                            {
                                UserName = cbxYhm.Text,
                                Email = tbEmail.Text,
                                Remark = tbRemark.Text
                            };
                            UserInfoHelper.UpdateUser(userinfo);
                            MessageBox.Show("修改成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            ClearCntrValue(this.gbUser);
                            Bindyhzname(cbxYhz);
                    }
                    break;
                case "Edit User":
                    if (cbxYhm.Text.Trim() == "")
                    {
                        MessageBox.Show("Username empty！", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (UserGroupHelper.UserGroupIsExist(cbxYhz.Text) == false)
                    {
                        MessageBox.Show("User group doesn't exist！", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (tbTel.Text.Trim() == "")
                    {
                        MessageBox.Show("TEL empty！", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (!Util.IsTel(tbTel.Text))
                    {
                        MessageBox.Show("Please input the correct TEL！", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (tbEmail.Text.Trim() == "")
                    {
                        MessageBox.Show("Email empty！", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (!Util.IsEmail(tbEmail.Text))
                    {
                        MessageBox.Show("Please input the correct email！", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Warning);//不匹配则弹出”邮箱错误“
                    }
                    else
                    {
                        UserInfo userinfo = new UserInfo()
                        {
                            UserName = cbxYhm.Text,
                            Email = tbEmail.Text,
                            Remark = tbRemark.Text
                        };
                        UserInfoHelper.UpdateUser(userinfo);
                        MessageBox.Show("Success", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        ClearCntrValue(this.gbUser);
                        Bindyhzname(cbxYhz);
                    }
                    break;
                case "删除用户":
                    if (cbxYhm.Text.Trim() == "")
                    {
                        MessageBox.Show("用户不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (UserGroupHelper.UserGroupIsExist(cbxYhz.Text) == false)
                    {
                        MessageBox.Show("用户组不存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (IndexPage.username == cbxYhm.Text)
                    {
                        MessageBox.Show("无法删除当前用户！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (MessageBox.Show("确定删除?", "提示", MessageBoxButtons.YesNo,MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        string username = cbxYhm.Text;
                        UserInfoHelper.DeleteuerByname(username);
                        MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        ClearCntrValue(this.gbUser);
                        Bindyhzname(cbxYhz);
                    }
                    break;
                case "Delete User":
                    if (cbxYhm.Text.Trim() == "")
                    {
                        MessageBox.Show("Username empty！", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (UserGroupHelper.UserGroupIsExist(cbxYhz.Text) == false)
                    {
                        MessageBox.Show("User group doesn't exist！", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (IndexPage.username == cbxYhm.Text)
                    {
                        MessageBox.Show("Delete failed！", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (MessageBox.Show("Delete?", "Tip", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        string username = cbxYhm.Text;
                        UserInfoHelper.DeleteuerByname(username);
                        MessageBox.Show("Success", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        ClearCntrValue(this.gbUser);
                        Bindyhzname(cbxYhz);
                    }
                    break;
            }
        }
        /// <summary>
        /// 用户组取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void groupcancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// 用户取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void usercancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// 修改用户时，从下拉框中选择用户名后显示相应信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void yhmCbx_SelectedIndexChanged(object sender, EventArgs e)
        {
            ClearCntrValue(this.gbUser);
            if (cbxYhm.SelectedIndex != -1)
            {
                string strSelUserName = cbxYhm.SelectedItem.ToString();
                if (strSelUserName != "")
                {
                    UserInfo userinfo = UserInfoHelper.RetrieveUserinfo(strSelUserName);
                    if (userinfo != null)
                    {
                        cbxYhz.Text = userinfo.Groupname;
                        tbPwd.Text = userinfo.PassWord;
                        tbConfirmpwd.Text = userinfo.PassWord;
                        tbTel.Text = userinfo.Telephone;
                        tbEmail.Text = userinfo.Email;
                        tbRemark.Text = userinfo.Remark;
                    }
                }
            }
        }
        /// <summary>
        /// 清除groupbox中控件里的内容
        /// </summary>
        /// <param name="parContainer"></param>
        public void ClearCntrValue(Control parContainer)
        {
            foreach (Control control in parContainer.Controls)
            {
                if (control is TextBox)
                {
                    control.Text = "";
                }
                else if (control is ComboBox)
                {
                    control.Text = "";
                }
            }
        }
        /// <summary>
        /// 获取勾选的菜单功能
        /// </summary>
        /// <param name="clb"></param>
        /// <returns></returns>
        private string GetChecked(CheckedListBox clb)
        {
            List<String> str = new List<string>();
            for (int i = 0; i < clb.CheckedItems.Count; i++)
            {
                str.Add(((Dicts)clb.CheckedItems[i]).Code.ToString());
            }
            return string.Join(",", str);
        }
        /// <summary>
        /// 根据用户组下拉框名称分别给两个选项卡中的用户组绑定数据源
        /// </summary>
        /// <param name="combobox"></param>
        private void Bindyhzname(ComboBox combobox)
        {
            List<UserGroup> yhzname = UserGroupHelper.RetrieveUsergroupinfo();
            combobox.DataSource = yhzname;
            combobox.DisplayMember = "GroupName";
            combobox.SelectedIndex = -1;
        }
        /// <summary>
        /// 将菜单功能的勾选项取消勾选
        /// </summary>
        private void authorityClb_ClearChecked()
        {
            for (int i = 0; i < clbAuthority.Items.Count; i++)
            {
                if (clbAuthority.GetItemChecked(i))
                {
                    clbAuthority.SetItemChecked(i, false);
                }
            }
            clbAuthority.SelectedIndex = -1;
        }
        /// <summary>
        /// 初始化全选checkbox
        /// </summary>
        private void allcheckCb_Clear()
        {
            if (cbAllcheck.Checked == true)
                cbAllcheck.Checked =false;
        }
        /// <summary>
        /// 选择某一用户组后显示对应的信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void groupyhzCbx_SelectedIndexChanged(object sender, EventArgs e)
        {
            authorityClb_ClearChecked();
            allcheckCb_Clear();
            if(cbxGroupyhz.SelectedIndex!=-1)
            {
                string strSelGroupName = ((UserGroup)cbxGroupyhz.SelectedItem).GroupName;
                if (strSelGroupName != "")
                {
                    string str = ((UserGroup)cbxGroupyhz.SelectedItem).PopedomCodeex;
                    if (str != "")
                    {
                        str.Split(',').ToList().ForEach(o => clbAuthority.SetItemChecked(int.Parse(o), true));
                    }
                }
                if (clbAuthority.CheckedIndices.Count == clbAuthority.Items.Count)
                {
                    cbAllcheck.Checked = true;
                }
            }
        }
        
        /// <summary>
        /// 为checklistbox绑定数据源
        /// </summary>
        private void BindMenu() 
        {
            List<Dicts> dict = DicHelper.RetrieveDict().Where(o => o.Category == "操作类型").ToList();
            clbAuthority.DataSource = dict;
            clbAuthority.ValueMember = "code";
            clbAuthority.DisplayMember = "name";
            
        }

        /// <summary>
        /// 根据选中的用户组查询到的用户加载到用户下拉框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void yhzCbx_SelectedIndexChanged(object sender, EventArgs e)
        {
            ClearCntrValue(this.gbUser);
            if (cbxYhz.SelectedIndex != -1)
            {
                string strSelGroupName = ((UserGroup)cbxYhz.SelectedItem).GroupName;
                if (strSelGroupName != "")
                {
                    List<String> username = UserInfoHelper.RetrieveUsernameBygroupname(strSelGroupName);
                    cbxYhm.DataSource = username;
                    cbxYhm.SelectedIndex = -1;
                }
            }
        }
        /// <summary>
        /// 判断勾选框是否有选项
        /// </summary>
        /// <returns></returns>
        private bool HasSelected()
        {
            bool SelFlag=false;
            for (int i = 0; i < clbAuthority.Items.Count; i++)
            {
                if (clbAuthority.GetItemChecked(i))
                {
                    SelFlag = true;
                    break;
                }
            }
            return SelFlag;
        }
        /// <summary>
        /// 全选和取消全选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void allcheckCb_CheckedChanged(object sender, EventArgs e)
        {
            if(cbAllcheck.CheckState==CheckState.Checked)
            {
                for (int i = 0; i < clbAuthority.Items.Count; i++)
                {
                    clbAuthority.SetItemChecked(i, true);
                }
            }
            else if (cbAllcheck.CheckState == CheckState.Unchecked)
            {
                for (int i = 0; i < clbAuthority.Items.Count; i++)
                {
                    clbAuthority.SetItemChecked(i, false);
                }
            }
        }

        #region 国际化
        /// <summary>
        /// 为当前页面内所有控件设置文本语种
        /// </summary>
        private void SetResourceCulture()
        {
            this.Text = ResourceCulture.GetString("UserManageText");
            this.tvUsergroup.Nodes[0].Text = ResourceCulture.GetString("AddUserGroup");
            this.tvUsergroup.Nodes[1].Text = ResourceCulture.GetString("EditUserGroup");
            this.tvUsergroup.Nodes[2].Text = ResourceCulture.GetString("DeleteUserGroup");
            this.tvUser.Nodes[0].Text = ResourceCulture.GetString("AddUser");
            this.tvUser.Nodes[1].Text = ResourceCulture.GetString("EditUser");
            this.tvUser.Nodes[2].Text = ResourceCulture.GetString("DeleteUser");
            this.gbGroup.Text = ResourceCulture.GetString("AddUserGroup");
            this.gbUser.Text = ResourceCulture.GetString("AddUser");
            this.tcUsermanagement.TabPages[0].Text = ResourceCulture.GetString("UserGroup");
            this.tcUsermanagement.TabPages[1].Text = ResourceCulture.GetString("User");
            this.lblGroupyhz.Text = ResourceCulture.GetString("UserGroup") + ":*";
            this.cbAllcheck.Text = ResourceCulture.GetString("SelectAll");
            this.lblGroupremark.Text = ResourceCulture.GetString("Remarks") + ":";
            this.lblYhz.Text = ResourceCulture.GetString("UserGroup") + ":*";
            this.lblYhm.Text = ResourceCulture.GetString("UserNameText") + ":*";
            this.lblPwd.Text = ResourceCulture.GetString("PasswordText") + ":*";
            this.lblConfirmpwd.Text = ResourceCulture.GetString("ComfirmPwd") + ":*";
            this.lblTel.Text = ResourceCulture.GetString("Tel") + ":*";
            this.lblRemark.Text = ResourceCulture.GetString("Remarks") + ":*";
            this.btnGroupsubmit.Text = ResourceCulture.GetString("Submit");
            this.btnGroupcancel.Text = ResourceCulture.GetString("CancelText");
            this.btnUsersubmit.Text = ResourceCulture.GetString("Submit");
            this.btnUsercancel.Text = ResourceCulture.GetString("CancelText");
        }
        #endregion

        private void UserManagement_Load(object sender, EventArgs e)
        {
            ResourceCulture.SetCurrentCulture(IndexPage.language);
            this.SetResourceCulture();
        }
    }
}
